<?php
class Upload
{
    //上传错误-路径不存在
    const UPLOAD_ERR_CUSTOM_PATH=-3;
    //上传错误-扩展名不正确
    const UPLOAD_ERR_CUSTOM_EXT=-2;
    //上传错误-文件大小不符合要求
    const UPLOAD_ERR_CUSTOM_SIZE=-1;

    //上传格式-图像
    public static $UPLOAD_EXT_IMAGE=array('gif','jpg','png','bmp');
    //上传格式-文件
    public static $UPLOAD_EXT_FILE=array('pdf','txt','doc','docx','xls','xlsx');

    /**
     * 上传文件
     */
    public function Execute($file,$path,$size=NULL,$expanded_name=NULL,$autoName=TRUE)
    {
        if($file['error']==UPLOAD_ERR_OK)
        {
            $fileName=$file['name'];
            if($autoName){
                $fileName=date('Ymd').time().rand(1000,2000).'.'.self::GetExtName($file);
            }

            if(@move_uploaded_file($file['tmp_name'],str_replace('//','/',$path).'/'.$fileName))
                return $fileName;            
            else
            {
                $error=error_get_last();       
                throw new Exception($error['message'].'。'.'发生错误的文件：'.$error['file'].'。'.'错误行号：'.$error['line'].'。');
            }                               
        }
        else
        {
            return array($file['error'],self::Error_Message($file['error']));
        }
    }

    /**
     * 检测
     */
    public function Probe($file,$size=NULL,$expanded_name=NULL,$path=NULL)
    {
        $result=array();
        if($file['error']!=0)
        {
            $result['error']=$file['error'];
            $result['message']=self::Error_Message($result['error']);
        }
        else
        {
            if($size&&$file['size']>$size)
            {
                $result['error']=self::UPLOAD_ERR_CUSTOM_SIZE;
            }
            else if($expanded_name)
            {
                if(gettype($expanded_name)!='array') {
                    $expanded_name=explode(',',$expanded_name);
                }

                $tmp=NULL;
                if(!in_array(self::GetExtName($file),$expanded_name)){
                    $tmp=self::UPLOAD_ERR_CUSTOM_EXT;
                }
                else{
                    $tmp=UPLOAD_ERR_OK;
                }
                $result['error']=$tmp;
            }
            else if($path&&!is_dir($path))
            {
                $result['error']=self::UPLOAD_ERR_CUSTOM_PATH;
            }
            else
            {
                $result['error']=UPLOAD_ERR_OK;
            }
            $result['message']=self::Error_Message($result['error']);
        }
        return $result;
    }

    /**
     * 错误索引
     */
    private function Error_Message($errorCode)
    {
        $message=array
        (
            //-3
            self::UPLOAD_ERR_CUSTOM_PATH=>'文件上传路径不存在。',
            //-2
            self::UPLOAD_ERR_CUSTOM_EXT=>'文件格式与上传配置不符。',
            //-1
            self::UPLOAD_ERR_CUSTOM_SIZE=>'文件大小超过了上传配置。',
            //0
            UPLOAD_ERR_OK=>'没有错误，上传成功。',
            //1
            UPLOAD_ERR_INI_SIZE=>'上传文件大小超出了php.ini中upload_max_filesize的值。',
            //2
            UPLOAD_ERR_FORM_SIZE=>'上传文件大小超出了表单中MAX_FILE_SIZE的值。',
            //3
            UPLOAD_ERR_PARTIAL=>'文件只上传了一部分，没有被全部上传。',
            //4
            UPLOAD_ERR_NO_FILE=>'文件域中没有文件。',
            //6
            UPLOAD_ERR_NO_TMP_DIR=>'找不到临时目录。',
            //7
            UPLOAD_ERR_CANT_WRITE=>'写入失败，可能没有权限。',
            //8
            UPLOAD_ERR_EXTENSION=>'文件上传过程中被停止。'
        );
        return $message[$errorCode];
    }

    /**
     * 获取扩展名
     */
    private function GetExtName($file)
    {
        return end(explode('.', $file['name']));
    }
}
?>